<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Prizes extends CI_Controller
{
    protected $menu = array(
	'menu_1' => '',
	'menu_2' => 'active',
	'menu_3' => '',
	'menu_4' => '',
    );
    
    function __construct()
    {
	parent::__construct();
	
	if( ! $this->_checkAuth()){
	    redirect('admin/login');
	    exit;
	}
    }
    
    function index()
    {
	$this->mytpl->set_template('admin-bootstrap/menu.html');
	$this->mytpl->clear_cache();
	$this->mytpl->set_var($this->menu);
	$data['menu'] = $this->mytpl->html();
	
	$this->initmodel->set_table_name('prizes');
	$this->initmodel->set_where(array('status'=>'1'));
	$var['prizes'] = $this->initmodel->load_records();
	
	$this->mytpl->set_template('admin-bootstrap/prizes.html');
	$this->mytpl->clear_cache();
	$this->mytpl->set_var($var);
	$data['content'] = $this->mytpl->html();
	
	$this->mytpl->set_template('admin-bootstrap/main.html');
	$this->mytpl->clear_cache();
	$this->mytpl->set_var($data);
	$this->mytpl->display();
    }
    
    function add_form()
    {
	$this->mytpl->set_template('admin-bootstrap/menu.html');
	$this->mytpl->clear_cache();
	$this->mytpl->set_var($this->menu);
	$data['menu'] = $this->mytpl->html();
	
	$this->mytpl->set_template('admin-bootstrap/prizes-add-form.html');
	$this->mytpl->clear_cache();
	$data['content'] = $this->mytpl->html();
	
	$this->mytpl->set_template('admin-bootstrap/main.html');
	$this->mytpl->clear_cache();
	$this->mytpl->set_var($data);
	$this->mytpl->display();
    }
    
    function add()
    {
	if ($this->input->post('post_check')) {
	    $input['amount'] = $this->input->post('amount');
	    $input['remark'] = $this->input->post('remark');
	    
	    $this->initmodel->set_table_name('prizes');
	    $prizes_id = $this->initmodel->insert_record($input);
	    
	    if($prizes_id){
		$this->load->library('ciupload',$_FILES['logo']);
		if ($this->ciupload->uploaded ) {
		    $this->ciupload->image_resize = true;
		    $this->ciupload->image_ratio_fill = true;
		    $this->ciupload->image_x = 207;
		    $this->ciupload->image_y = 209;
		    $this->ciupload->process(FCPATH."assets/images/prizes/{$prizes_id}/");
		    
		    if($this->ciupload->processed){
			$this->ciupload->clean();
			$input['logo'] = $this->ciupload->file_dst_name;
		    }
		}
		
		$this->ciupload->upload($_FILES['detail_img']);
		if ($this->ciupload->uploaded ) {
		    $this->ciupload->image_resize = true;
		    $this->ciupload->image_ratio_fill = true;
		    $this->ciupload->image_x = 400;
		    $this->ciupload->image_y = 205;
		    $this->ciupload->process(FCPATH."assets/images/prizes/{$prizes_id}/");
		    
		    if($this->ciupload->processed){
			$this->ciupload->clean();
			$input['detail_img'] = $this->ciupload->file_dst_name;
		    }
		}
		
		$this->initmodel->set_where(array('id'=>$prizes_id));
		$this->initmodel->update_record($input);
	    }
	}
	
	redirect('admin/prizes');
    }
    
    function edit_form($prizes_id)
    {
	$this->mytpl->set_template('admin-bootstrap/menu.html');
	$this->mytpl->clear_cache();
	$this->mytpl->set_var($this->menu);
	$data['menu'] = $this->mytpl->html();
	
	$this->initmodel->set_table_name('prizes');
	$this->initmodel->set_where(array('id'=>$prizes_id));
	$prizes = $this->initmodel->load_records();
	
	$var['id'] = $prizes['0']['id'];
	$var['logo'] = $prizes['0']['logo'];
	$var['detail_img'] = $prizes['0']['detail_img'];
	$var['amount'] = $prizes['0']['amount'];
	$var['remark'] = $prizes['0']['remark'];
	
	$this->mytpl->set_template('admin-bootstrap/prizes-edit-form.html');
	$this->mytpl->clear_cache();
	$this->mytpl->set_var($var);
	$data['content'] = $this->mytpl->html();
	
	$this->mytpl->set_template('admin-bootstrap/main.html');
	$this->mytpl->clear_cache();
	$this->mytpl->set_var($data);
	$this->mytpl->display();
    }
    
    function edit()
    {
	if ($this->input->post('post_check')) {
	    $prizes_id = $this->input->post('prizes_id');
	    $input['amount'] = $this->input->post('amount');
	    $input['remark'] = $this->input->post('remark');
	    
	    $this->load->library('ciupload',$_FILES['logo']);
	    if ($this->ciupload->uploaded ) {
		$this->ciupload->image_resize = true;
		$this->ciupload->image_ratio_fill = true;
		$this->ciupload->image_x = 207;
		$this->ciupload->image_y = 209;
		$this->ciupload->process(FCPATH."assets/images/prizes/{$prizes_id}/");
		
		if($this->ciupload->processed){
		    $this->ciupload->clean();
		    $input['logo'] = $this->ciupload->file_dst_name;
		}
	    }
	    
	    $this->ciupload->upload($_FILES['detail_img']);
	    if ($this->ciupload->uploaded ) {
		$this->ciupload->image_resize = true;
		$this->ciupload->image_ratio_fill = true;
		$this->ciupload->image_x = 400;
		$this->ciupload->image_y = 205;
		$this->ciupload->process(FCPATH."assets/images/prizes/{$prizes_id}/");
		
		if($this->ciupload->processed){
		    $this->ciupload->clean();
		    $input['detail_img'] = $this->ciupload->file_dst_name;
		}
	    }
	    
	    $this->initmodel->set_table_name('prizes');
	    $this->initmodel->set_where(array('id'=>$prizes_id));
	    $this->initmodel->update_record($input);
	}
	
	redirect('admin/prizes');
    }
    
    function delete($id)
    {
	$data['status'] = '0';
	$this->initmodel->set_table_name('prizes');
	$this->initmodel->set_where(array('id'=>$id));
	$this->initmodel->update_record($data);
	redirect('admin/prizes');
    }
    
    /**
    * private function to use in this controller
    */
    function _checkAuth()
    {
	$admin = $this->session->userdata('admin');
	if (isset($admin['user']) && $admin['user'] == 'admin') {
	    return true;
	} else {
	    return false;
	}
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */